package com.qianxx.component.activity.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.qianxx.component.R;
import com.qianxx.component.tool.SystemBarTintManager;
import com.qianxx.component.tool.TitleBar;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class BaseActivity extends AppCompatActivity implements TitleBar.OnClickListener {
    LinearLayout parentLinearLayout;
    RelativeLayout childRelativeLayout;
    TitleBar title_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //永远设置为竖屏
//        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }

        //设置状态栏的样式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(setEnabledStatusBar());
            tintManager.setStatusBarTintResource(setBarColor());
        }

        //添加布局
        initContentView(R.layout.activity_base);

        //设置标题
        title_bar = (TitleBar) findViewById(R.id.title);
        title_bar.setVisibility(setEnableTitleBar() ? View.VISIBLE : View.GONE);
        title_bar.setCenterText(setTitle());
        title_bar.setLeftText(setLeftTitle());
        title_bar.setRightText(setRightTitle());
        title_bar.setCenterRes(setCenterRes());
        title_bar.setLeftRes(setLeftRes());
        title_bar.setRightRes(setRightRes());
        title_bar.setOnClickListener(this);
        title_bar.setLeftVisiable(setLeftVisiable());
    }

    //初始化content的View
    private void initContentView(int layoutResID) {
        //得到窗口的根布局
        ViewGroup group = (ViewGroup) findViewById(android.R.id.content);
        //首先先移除在根布局上的组件
        group.removeAllViews();
        //创建自定义父布局
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        parentLinearLayout = new LinearLayout(this);
        parentLinearLayout.setLayoutParams(params);
        parentLinearLayout.setOrientation(LinearLayout.VERTICAL);
        //将自定义的父布局，加载到窗口的根布局上
        group.addView(parentLinearLayout);
        //将自定义的标题栏布局加到parentLinearLayout上
        parentLinearLayout.addView(LayoutInflater.from(this).inflate(layoutResID, null, false));
        //自定义子布局，用来添加布局文件里的布局
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        childRelativeLayout = new RelativeLayout(this);
        childRelativeLayout.setLayoutParams(layoutParams);
        parentLinearLayout.addView(childRelativeLayout);
    }

    @Override
    public void setContentView(int layoutResID) {
        //super.setContentView(layoutResID);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        View view = LayoutInflater.from(this).inflate(layoutResID, null, false);
        view.setLayoutParams(layoutParams);
        childRelativeLayout.addView(view);
    }

    @Override
    public void setContentView(View view) {
        //super.setContentView(view);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        childRelativeLayout.addView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        //super.setContentView(view, params);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        childRelativeLayout.addView(view);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 状态栏亮色模式，设置状态栏黑色文字、黑色图标，
     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
     *
     * @param activity
     * @param isDark   是否是黑色字体和图标
     * @return 1:MIUUI 2:Flyme 3:android6.0
     */
    public int statusBarLightMode(Activity activity, boolean isDark) {
        int result = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (MIUISetStatusBarLightMode(activity, isDark)) {
                result = 1;
            } else if (FlymeSetStatusBarLightMode(activity.getWindow(), isDark)) {
                result = 2;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (isDark) {
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                } else {
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                }
                result = 3;
            }
        }
        return result;
    }

    /**
     * 设置状态栏图标为深色和魅族特定的文字风格
     * 可以用来判断是否为Flyme用户
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏文字及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    private boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 需要MIUIV6以上
     *
     * @param activity
     * @param dark     是否把状态栏文字及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    private boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) {
        boolean result = false;
        Window window = activity.getWindow();
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result = true;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //开发版 7.7.13 及以后版本采用了系统API，旧方法无效但不会报错，所以两个方式都要加上
                    if (dark) {
                        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    } else {
                        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                    }
                }
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 获取状态栏颜色
     *
     * @return
     */
    public int setBarColor() {
        return R.color.black;
    }

    /**
     * 设置是否启用状态栏
     *
     * @return
     */
    public boolean setEnabledStatusBar() {
        return true;
    }

    /**
     * 设置标题
     *
     * @return
     */
    public String setTitle() {
        return "控件大全";
    }

    /**
     * 设置标题栏左标题
     *
     * @return
     */
    public String setLeftTitle() {
        return "";
    }

    /**
     * 设置标题栏右标题
     *
     * @return
     */
    public String setRightTitle() {
        return "";
    }

    /**
     * 标题栏左点击事件
     *
     * @param v
     */
    @Override
    public void onLeftClick(View v) {
        finish();
    }

    /**
     * 标题点击事件
     *
     * @param v
     */
    @Override
    public void onCenterClick(View v) {
    }

    /**
     * 标题栏右点击事件
     *
     * @param v
     */
    @Override
    public void onRightClick(View v) {
    }

    /**
     * 设置左边按钮是否为可见
     */
    public boolean setLeftVisiable() {
        return true;
    }

    /**
     * 设置标题栏左图标
     */
    public int setLeftRes() {
        return R.drawable.fanhui;
    }

    /**
     * 设置标题栏右图标
     */
    public int setRightRes() {
        return 0;
    }

    /**
     * 设置标题栏中间图标
     */
    public int setCenterRes() {
        return 0;
    }

    /**
     * 是否启用标题栏
     */
    public boolean setEnableTitleBar() {
        return true;
    }

}
